﻿using System;
using Gumini.JulaDB.Enums;
using Gumini.JulaDB.Provider;

namespace Gumini.JulaDB.Expressions.Operators
{
    internal abstract class ComparisonOperator : RelationalOperator
    {
        public ComparisonOperator(params string[] names)
            : base(names) { }

        protected abstract bool GetComparisonValue(int compareToResult);

        protected override bool EvaluateCore(object a, object b)
        {
            var aComparable = a as IComparable;
            var bComparable = b as IComparable;
            if (aComparable == null || bComparable == null)
                throw new JulaDbException(ErrorCode.UnableToCompare, "Unable to compare two expressions.");
            int compareToResult = aComparable.CompareTo(bComparable);
            return GetComparisonValue(compareToResult);
        }
    }
}
